package com.bailian.snails.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;

/**
 * @author gaoxu
 * date: Created on 2021/5/31 下午3:19
 */
//@Configuration
public class RestTemplateConfig {


    @Autowired
    LoadBalancerClient loadBalancer;

    /**
     * 两种方式:
     *  1.显示使用注入
     *  2.注解的方式
     *
     * @return
     */
    @Bean
    @LoadBalanced
    public RestTemplate restTemplate() {
        return new RestTemplate();

        /**
         * @LoadBalanced 可以实现这个功能
            RestTemplate restTemplate = new RestTemplate();
            restTemplate.setInterceptors(Collections.singletonList(
                    new LoadBalancerInterceptor(loadBalancer)
            ));
            return restTemplate;
         */
    }
}
